﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Windows.Forms;

namespace luanvanFinal
{
    class clsProject
    {
        int id;
        String proName;
        String proPM;
        String proCustomer;
        Nullable <DateTime> proStartDay=null;
        Nullable <DateTime> proEndDay = null;
        Nullable <DateTime> proProgress1=null;
        Nullable <DateTime> proProgress2=null;
        Nullable <DateTime> proProgress3=null;
        Nullable <DateTime> proProgress4=null;
        Nullable <DateTime> proProgress5=null;
        Nullable <DateTime> proProgress6=null;
        String proNote;

        public int ID
        {
            get { return id; }
            set { id = value; }
        }

        public String ProName
        {
            get { return proName; }
            set { proName = value; }
        }

        public String ProPM
        {
            get { return proPM; }
            set { proPM = value; }
        }

        public String ProCustomer
        {
            get { return proCustomer; }
            set { proCustomer = value; }
        }

        public Nullable <DateTime> ProStartDay
        {
            get { return proStartDay; }
            set { proStartDay = value; }
        }

        public Nullable<DateTime> ProEndDay
        {
            get { return proEndDay; }
            set { proEndDay = value; }
        }

        public Nullable<DateTime> ProProgress1
        {
            get { return proProgress1; }
            set { proProgress1 = value; }
        }

        public Nullable<DateTime> ProProgress2
        {
            get { return proProgress2; }
            set { proProgress2 = value; }
        }

        public Nullable<DateTime> ProProgress3
        {
            get { return proProgress3; }
            set { proProgress3 = value; }
        }

        public Nullable<DateTime> ProProgress4
        {
            get { return proProgress4; }
            set { proProgress4 = value; }
        }

        public Nullable<DateTime> ProProgress5
        {
            get { return proProgress5; }
            set { proProgress5 = value; }
        }

        public Nullable<DateTime> ProProgress6
        {
            get { return proProgress6; }
            set { proProgress6 = value; }
        }

        public String ProNote
        {
            get { return proNote; }
            set { proNote = value; }
        }

        public clsProject()
        {
            id = 0;
            proName = "";
            proPM = "";
            proCustomer = "";
            proStartDay = null;
            proEndDay = null;
            proProgress1 = null;
            proProgress2 = null;
            proProgress3 = null;
            proProgress4 = null;
            proProgress5 = null;
            proProgress6 = null;
            proNote = "";
        }

        public void insertProject()
        {
            clsConnect ketnoi = new clsConnect();
            ketnoi.openConnection();
            try
            {
                SqlCommand sqlcmd = new SqlCommand("insertProject", ketnoi.Connection);
                sqlcmd.CommandType = CommandType.StoredProcedure;
                sqlcmd.Parameters.Add("@proName", SqlDbType.NVarChar).Value = this.proName;
                sqlcmd.Parameters.Add("@proPM", SqlDbType.NVarChar).Value = this.proPM;
                sqlcmd.Parameters.Add("@proCustomer", SqlDbType.NVarChar).Value = this.proCustomer;
                sqlcmd.Parameters.Add("@proStartDay", SqlDbType.DateTime).Value = this.proStartDay;
                sqlcmd.Parameters.Add("@proEndDay", SqlDbType.DateTime).Value = this.proEndDay;
                sqlcmd.Parameters.Add("@proProgress1", SqlDbType.DateTime).Value = this.proProgress1;
                sqlcmd.Parameters.Add("@proProgress2", SqlDbType.DateTime).Value = this.proProgress2;
                sqlcmd.Parameters.Add("@proProgress3", SqlDbType.DateTime).Value = this.proProgress3;
                sqlcmd.Parameters.Add("@proProgress4", SqlDbType.DateTime).Value = this.proProgress4;
                sqlcmd.Parameters.Add("@proProgress5", SqlDbType.DateTime).Value = this.proProgress5;
                sqlcmd.Parameters.Add("@proProgress6", SqlDbType.DateTime).Value = this.proProgress6;
                sqlcmd.Parameters.Add("@proNote", SqlDbType.NVarChar).Value = this.proNote;

                sqlcmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Dữ liệu đã tồn tại hoặc không hợp lệ!!!" + ex.ToString(), "Warning", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            ketnoi.closeConnection();
        }

        public void updateProject()
        {
            clsConnect ketnoi = new clsConnect();
            ketnoi.openConnection();
            try
            {
            SqlCommand sqlcmd = new SqlCommand("updateProject", ketnoi.Connection);
            sqlcmd.CommandType = CommandType.StoredProcedure;
            sqlcmd.Parameters.Add("@ID", SqlDbType.Int).Value = this.id;
            sqlcmd.Parameters.Add("@proName", SqlDbType.NVarChar).Value = this.proName;
            sqlcmd.Parameters.Add("@proPM", SqlDbType.NVarChar).Value = this.proPM;
            sqlcmd.Parameters.Add("@proCustomer", SqlDbType.NVarChar).Value = this.proCustomer;
            sqlcmd.Parameters.Add("@proStartDay", SqlDbType.DateTime).Value = this.proStartDay;
            sqlcmd.Parameters.Add("@proEndDay", SqlDbType.DateTime).Value = this.proEndDay;
            sqlcmd.Parameters.Add("@proProgress1", SqlDbType.DateTime).Value = this.proProgress1;
            sqlcmd.Parameters.Add("@proProgress2", SqlDbType.DateTime).Value = this.proProgress2;
            sqlcmd.Parameters.Add("@proProgress3", SqlDbType.DateTime).Value = this.proProgress3;
            sqlcmd.Parameters.Add("@proProgress4", SqlDbType.DateTime).Value = this.proProgress4;
            sqlcmd.Parameters.Add("@proProgress5", SqlDbType.DateTime).Value = this.proProgress5;
            sqlcmd.Parameters.Add("@proProgress6", SqlDbType.DateTime).Value = this.proProgress6;
            sqlcmd.Parameters.Add("@proNote", SqlDbType.NVarChar).Value = this.proNote;
            sqlcmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
            MessageBox.Show("Dữ liệu đã tồn tại hoặc không hợp lệ!!!" + ex.ToString(), "Warning", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            

            ketnoi.closeConnection();
        }
        public String getIdFromName(string name)
        {
            try
            {
                String strSQL = "select ID from project where proName = '" + name + "'";
                clsConnect ketnoi = new clsConnect();
                ketnoi.openConnection();
                SqlDataAdapter da = new SqlDataAdapter(strSQL, ketnoi.Connection);
                DataTable dt = new DataTable();
                da.Fill(dt);
                ketnoi.closeConnection();
                return dt.Rows[0][0].ToString();
            }
            catch
            {
                return null;
            }
        }
        public String getNameFromId(int id)
        {
            try
            {
                String strSQL = "select proName from project where ID = '" + id + "'";
                clsConnect ketnoi = new clsConnect();
                ketnoi.openConnection();
                SqlDataAdapter da = new SqlDataAdapter(strSQL, ketnoi.Connection);
                DataTable dt = new DataTable();
                da.Fill(dt);
                ketnoi.closeConnection();
                return dt.Rows[0][0].ToString();
            }
            catch
            {
                return null;
            }
        }


        public DataTable getDataProject()
        {
            String strSQL = "select * from project";
            clsConnect ketnoi = new clsConnect();
            ketnoi.openConnection();
            SqlDataAdapter da = new SqlDataAdapter(strSQL, ketnoi.Connection);
            DataTable dt = new DataTable();
            da.Fill(dt);
            ketnoi.closeConnection();
            return dt;
        }

        public DataTable getDataProject(String strSQL)
        {
            clsConnect ketnoi = new clsConnect();
            ketnoi.openConnection();
            SqlDataAdapter da = new SqlDataAdapter(strSQL, ketnoi.Connection);
            DataTable dt = new DataTable();
            da.Fill(dt);
            ketnoi.closeConnection();
            return dt;
        }

        public DataTable getProjectName()
        {
            String strSQL = "select proName from project";
            clsConnect ketnoi = new clsConnect();
            ketnoi.openConnection();
            SqlDataAdapter da = new SqlDataAdapter(strSQL, ketnoi.Connection);
            DataTable dt = new DataTable();
            da.Fill(dt);
            ketnoi.closeConnection();
            return dt;
        }


        public void deleteProject()
        {
            clsConnect ketnoi = new clsConnect();
            ketnoi.openConnection();
            try
            {
                SqlCommand sqlcmd = new SqlCommand("deleteProject", ketnoi.Connection);
                sqlcmd.CommandType = CommandType.StoredProcedure;
                sqlcmd.Parameters.Add("@ID", SqlDbType.Int).Value = this.id;
                sqlcmd.ExecuteNonQuery();
                //MessageBox.Show("You have delete success !!!", "Success",MessageBoxButtons.OK,MessageBoxIcon.Information);
            }
            catch
            {
                MessageBox.Show("You have error while deleting project!!!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            ketnoi.closeConnection();
        }
    }
}
